package chances.webui.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.VelocityEngine;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

@SuppressWarnings("serial")
public class WebUIServlet extends HttpServlet{
	private String name ="webui/javascript.vm";
	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		WebApplicationContext webAppContext = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
		VelocityEngine velocityEngine = webAppContext.getBean(VelocityEngine.class);
		Template template = velocityEngine.getTemplate(name);
		VelocityContext context = new VelocityContext();
		resp.setContentType("text/javascript; charset=UTF-8");
		context.put("request", req);
		context.put("response",resp);
		context.put("contextPath", req.getContextPath());
		template.merge(context,resp.getWriter());	
	}

}
